#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>

void usage (char * prog) {
    fprintf (stderr, "USAGE: %s <input> <key>\n", basename(prog));
    fprintf (stderr, "------------------------------\n");
    fprintf (stderr, " input   The file with text to XOR with the key\n");
    fprintf (stderr, " key     The file with the key stream\n");
    exit (42);
}

int main (int argc, char ** argv) {

    if (argc != 3) usage(argv[0]);
    FILE * orig = fopen(argv[1], "rb");
    FILE * key = fopen(argv[2], "rb");
    while (! feof (orig)) {
        int c = fgetc (orig);
        int k = fgetc (key);
        if (c == EOF || k == EOF)
            break;
        printf ("%c", (unsigned char)c ^ (unsigned char)k);
    }

    fclose (orig);
    fclose (key);

    return 0;
}
